<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\TradeOnline as TradeOnlineModel;
use app\common\model\TradeOnlineOrder;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\Response;

class TradeOnline extends Base
{
    public function index(): Response
    {
        $result['account'] = [
            'title' => get_pay_way('usdt'),
            'type' => 'usdt',
            'balance' => $this->user->assets->usdt
        ];
        $result['method'] = [
            'title' => get_pay_way('currency'),
            'type' => 'currency',
            'balance' => $this->user->assets->currency
        ];
        $result['type'] = [
            [
                'id' => 1,
                'title' => getTradeType(1)
            ],
            [
                'id' => 2,
                'title' => getTradeType(2)
            ]
        ];
        $result['factor'] = [
            'price' => $this->system['usdt_price'],
            'trade_open' => $this->system['trade_online_open'],
            'trade_publish_min' => $this->system['trade_online_publish_min'],
            'trade_publish_bs' => $this->system['trade_online_publish_bs'],
            'trade_min' => $this->system['trade_online_min'],
            'trade_bs' => $this->system['trade_online_bs'],
            'trade_cost' => $this->system['trade_online_cost']
        ];

        return Result::success('success', $result);
    }

    public function hallList(): Response
    {
        $param = $this->request->get();
        $map['status'] = 1;
        if (!empty($param['type'])) {
            $map['type_id'] = $param['type'];
        }
        $result = TradeOnlineModel::getPageList($map,
            'id,type_id,amount,surplus_amount as amount,price,create_time',
            $this->getPage(), $this->getLimit(), false, [], [], [], [], [], '', ['type_name']);
        foreach ($result['data'] as &$item) {
            $item->merit = round($item->amount * $item->price, 2);
        }

        return Result::fastReturn($result);
    }

    public function save(): Response
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'TradeOnline');
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }

        return Result::fastReturn(TradeOnlineModel::add($param, $this->user, $this->system));
    }

    public function record(): Response
    {
        $param = $this->request->get();
        $map['uid'] = $this->user->id;
        if (!empty($param['type'])) {
            $map['type_id'] = $param['type'];
        }
        if (!empty($param['status'])) {
            $map['status'] = $param['status'];
        }
        $result = TradeOnlineModel::getPageList($map,
            'id,type_id,sn,account,amount,surplus_amount,price,status,finish_time,cancel_time,create_time',
            $this->getPage(), $this->getLimit(), false, [], [], [], [], [], '', ['type_name', 'status_name']);
        foreach ($result['data'] as &$item) {
            $item->total_merit = round($item->amount * $item->price, 2);
            $item->surplus_merit = round($item->surplus_amount * $item->price, 2);
        }

        return Result::fastReturn($result);
    }

    public function revoke(): Response
    {
        $param = $this->request->post();

        return Result::fastReturn(TradeOnlineModel::revoke($param, $this->user));
    }

    public function add(): Response
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'TradeOnlineOrder');
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }

        return Result::fastReturn(TradeOnlineOrder::add($param, $this->user, $this->system));
    }

    public function myBuy(): Response
    {
        $map['buyer_id'] = $this->user->id;

        return Result::fastReturn(TradeOnlineOrder::getPageList($map,
            'id,sn,seller_id,account,amount,price,real_amount,create_time',
            $this->getPage(), $this->getLimit(), false, ['seller']));
    }

    public function mySell(): Response
    {
        $map['seller_id'] = $this->user->id;

        return Result::fastReturn(TradeOnlineOrder::getPageList($map,
            'id,sn,buyer_id,account,amount,price,real_amount,create_time',
            $this->getPage(), $this->getLimit(), false, ['buyer']));
    }
}